const db = require('../config/database');
const bcrypt = require('bcryptjs');

class Admin {
    static async create(adminData) {
        const { admin_name, password, email, role } = adminData;
        const hashedPassword = await bcrypt.hash(password, 10);
        const [result] = await db.execute(
            'INSERT INTO admins (admin_name, password, email, role) VALUES (?, ?, ?, ?)',
            [admin_name, hashedPassword, email, role]
        );
        return result.insertId;
    }

    static async findByEmail(email) {
        const [rows] = await db.execute('SELECT * FROM admins WHERE email = ?', [email]);
        return rows[0];
    }

    static async validatePassword(plainPassword, hashedPassword) {
        return await bcrypt.compare(plainPassword, hashedPassword);
    }
}

module.exports = Admin; 